package google.sites.view;

import dev.tools.design.patterns.state.IStateHandler;
import dev.tools.design.patterns.state.StateChanged;
import google.sites.model.Model;
import google.sites.view.dialogs.LoginDialog;
import google.sites.view.menus.MenuBar;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Nuno
 * Date: 01-10-2013
 * Time: 16:29
 */
public class View extends JFrame {

    public static final View INSTANCE = new View();

    private View() {
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setJMenuBar(new MenuBar());

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (JOptionPane.showConfirmDialog(View.this, "Are you sure?", "Closing", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                    System.exit(0);
            }
        });

        setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

        Tree pagesTreeView = new Tree();

        final JSplitPane splitPane = new JSplitPane();

        splitPane.setLeftComponent(pagesTreeView);
        final JTabbedPane webPagesTabbedPane = new JTabbedPane();
        splitPane.setRightComponent(webPagesTabbedPane);
        splitPane.setDividerLocation(150 + splitPane.getInsets().left);
        getContentPane().add(splitPane);

        final Map<String, WebPageView> openedWebPages = new LinkedHashMap<>();
        pagesTreeView.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                Object[] path = e.getPath().getPath();
                Tree.Node node = (Tree.Node) path[path.length - 1];
                WebPageView webPage;

                if (openedWebPages.containsKey(node.id))
                    webPage = openedWebPages.get(node.id);
                else{
                    int location = splitPane.getDividerLocation();
                    webPage = new WebPageView(node.getEntry());
                    webPagesTabbedPane.add(node.getTitle(), webPage);
                    openedWebPages.put(node.id, webPage);
                    splitPane.setDividerLocation(location);
                }


                webPagesTabbedPane.setSelectedComponent(webPage);
            }
        });
    }

    public static void main(String... args) {
        if (Model.INSTANCE.isState(Model.ABORTED))
            return;

        INSTANCE.launch();
    }

    private void launch() {
        Model.INSTANCE.addHandler(new IStateHandler<Model, Model.State>() {
            @Override
            public void update(Model context, StateChanged stateChanged) {
                setVisible(true);
            }
        }, Model.LOGGED_IN);
        new LoginDialog().setVisible(true);
    }

    public static void showError(Exception e) {
        JOptionPane.showMessageDialog(INSTANCE, e.getMessage(), e.getClass().getSimpleName(), JOptionPane.ERROR_MESSAGE);
    }

    public static boolean confirm(String message, String title) {
        return JOptionPane.showConfirmDialog(View.INSTANCE, message, title, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
    }
}
